const path = require("path");
const Mock = require("./mock");
var nodeExternals = require("webpack-node-externals");

const resolve = dir => {
    return path.join(__dirname, dir);
};

const BASE_URL = process.env.NODE_ENV === "production" ? "/portal/" : "/";
const pages =
    process.env.NODE_ENV === "production"
        ? {
              index: {
                  entry: "packages/index.js",
                  template: "public/index.html",
                  filename: "index.html",
              },
          }
        : undefined;

module.exports = {
    pages,
    publicPath: BASE_URL,
    productionSourceMap: false,
    //transpileDependencies: ["ant-design-vue"],
    chainWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            config.module
                .rule("js")
                .include.add("/packages/")
                .end()
                .use("babel")
                .loader("babel-loader")
                .tap(options => {
                    return options;
                });
        }

        config.entry("polyfill").add("@babel/polyfill");
        config.resolve.alias
            .set("@", resolve("src")) // key,value自行定义，比如.set('@@', resolve('src/components'))
            .set("_c", resolve("src/components"));
    },
    configureWebpack: {
        // externals: process.env.NODE_ENV === "production" ? getExternals() : { echarts: "echarts"},
        externals: process.env.NODE_ENV === "production" ? [nodeExternals(), { echarts: "echarts" }] : { echarts: "echarts" },
    },
    devServer: {
        contentBase: path.join(__dirname, "mock"),
        compress: true,
        host: process.env.VUE_APP_HOST,
        port: process.env.VUE_APP_PORT,
        hotOnly: true,
        overlay: {
            warnings: false,
            errors: true,
        },
        before(app) {
            Mock(app);
        },
    },
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    "primary-color": "#1890ff",
                    "link-color": "#1890ff",
                },
                javascriptEnabled: true,
            },
        },
    },
};
